package com.example.fbi_text.views;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.fbi_text.Acitivity.ProductDetailActivity;
import com.example.fbi_text.R;
import com.example.fbi_text.adapter.ProductAdapter;
import com.example.fbi_text.bean.Product;
import com.example.fbi_text.database.DBHelper;

import java.util.ArrayList;
import java.util.List;

public class ProductListFragment extends Fragment {
    private RecyclerView recyclerView;
    private ProductAdapter productAdapter;
    private List<Product> productList = new ArrayList<>();
    private DBHelper dbHelper; // 数据库帮助类实例

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_product_list, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // 初始化 RecyclerView
        recyclerView = view.findViewById(R.id.product_recycler_view);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));

        // 检查 getContext() 是否为 null
        if (getContext() != null) {
            dbHelper = DBHelper.getInstance(getContext()); // 初始化数据库帮助类
            loadProductsFromDatabase(); // 从数据库加载商品数据

            productAdapter = new ProductAdapter(getContext(), productList, product -> {
                Intent intent = new Intent(getActivity(), ProductDetailActivity.class);
                intent.putExtra("product", product);
                startActivity(intent);
            });
            recyclerView.setAdapter(productAdapter);
        } else {
            // 如果 getContext() 为 null，延迟初始化
            new Handler().postDelayed(() -> {
                if (isAdded() && getContext() != null) {
                    dbHelper = DBHelper.getInstance(getContext());
                    loadProductsFromDatabase();
                    productAdapter = new ProductAdapter(getContext(), productList, product -> {
                        Intent intent = new Intent(getActivity(), ProductDetailActivity.class);
                        intent.putExtra("product", product);
                        startActivity(intent);
                    });
                    recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
                    recyclerView.setAdapter(productAdapter);
                }
            }, 500); // 延迟 500 毫秒
        }
    }

    // 从数据库加载商品数据
    private void loadProductsFromDatabase() {
        if (dbHelper != null) {
            productList.clear();
            List<Product> dbProducts = dbHelper.getAllProducts();
            if (dbProducts != null && !dbProducts.isEmpty()) {
                productList.addAll(dbProducts);
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        loadProductsFromDatabase();
        if (productAdapter != null) {
            productAdapter.notifyDataSetChanged(); // 确保适配器更新
        }
    }
}